package com.hxzy.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.controller.admin.contamer.req.ContamerReq;
import com.hxzy.controller.mobile.login.req.MobileLoginReq;
import com.hxzy.controller.mobile.login.resp.MobileLoginResp;
import com.hxzy.controller.mobile.mapstruct.ContamerConvert;
import com.hxzy.entity.Contamer;
import com.hxzy.service.ContamerService;
import com.hxzy.mapper.ContamerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【contamer(前端用户)】的数据库操作Service实现
* @createDate 2023-06-28 16:38:44
*/
@Service
public class ContamerServiceImpl extends ServiceImpl<ContamerMapper, Contamer>
    implements ContamerService{

    @Autowired
    private ContamerMapper contamerMapper;

    @Override
    public PageInfo<Contamer> search(ContamerReq req) {

        return PageHelper.startPage(req.getPage(),req.getSize())
                .doSelectPageInfo(()-> {
                    //使用mybatis方法来实现
                    contamerMapper.search(req);
                });
    }

    /**
     * 本月注册的会员数量
     * @return
     */
    @Override
    public long currentMonthRegister() {
        //这样会使用索引失效   where  year(create_time)=yaer(now()) and month(create_time)=month(now())
        //只能这样做         where create_time  between  ?  and ?
        //使用hutool给我提供的工具类
       Date begin=DateUtil.beginOfMonth(new Date());
       Date end= DateUtil.endOfDay(DateUtil.endOfMonth(new Date()));

        LambdaQueryWrapper<Contamer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.between(Contamer::getCreateTime, begin,end);

        return super.count(queryWrapper);
    }

    @Override
    public List<Map> currentReportYear(int year) {
        return this.contamerMapper.currentReportYear(year);
    }

    @Override
    public long existsLoginId(String loginid) {
        LambdaQueryWrapper<Contamer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Contamer::getLoginId, loginid);
        long count = this.count(queryWrapper);
        return count;
    }

    @Override
    public MobileLoginResp login(MobileLoginReq req) {
        LambdaQueryWrapper<Contamer> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Contamer::getLoginId, req.getLoginName());
        Contamer db = super.getOne(queryWrapper);
        if(Objects.isNull(db)){
            throw new ServiceException(AckCode.USER_PASSWORD_NOT_FOUND);
        }

        boolean success= BCrypt.checkpw(req.getLoginPwd(),db.getPassword());

        if(!success){
            throw new ServiceException(AckCode.USER_PASSWORD_NOT_FOUND);
        }

        if(db.getStatus().equals("1")){
            throw new ServiceException(AckCode.DEVICE_BANNED);
        }
        return ContamerConvert.INSTANCE.convert(db);
    }
}




